/**
 * @BelongsProject: zzyl-cp423
 * @BelongsPackage: com.zzyl.nursing.service.impl
 * @Author: 王明天
 * @CreateTime: 2025/1/9 16:37
 * @Description: WechatServiceImpl
 */
package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.exception.base.BaseException;

import com.zzyl.common.utils.http.HttpUtils;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {


    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;



    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


    /**
     * 获取小程序中的openid
     */
    @Override
    public String getOpenid(String code) {

        //获取公共参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);
        paramMap.put("js_code", code);

        String result = HttpUtil.get(REQUEST_URL, paramMap);

        //将结果转换成JSON字符串
        JSONObject jsonObject = JSONUtil.parseObj(result);

        //判断接口是否出错
        if (ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {

            //如果不为空，代表出错了
            throw new BaseException(jsonObject.getStr("errmsg"));

        }

        return jsonObject.getStr("openid");


    }

    /**
     * 封装公共参数
     *
     * @return
     */
   /* private Map<String, Object> getAppConfig() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);
        return paramMap;
    }*/

    /**
     * 获取小程序中的手机号码
     */
    @Override
    public String getPhoneNumber(String detailCode) {

        //调用方法，获取accessToken
        String token = getToken();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", detailCode);

        //调用微信接口，获取手机号码,同时JSON.toJSONString(paramMap)将Map集合转换成JSON字符串,结果是字符串
        String result = HttpUtil.post(PHONE_REQUEST_URL + token, JSON.toJSONString(paramMap));


        //现将结果转换成JSON对象
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("errcode") != 0) {
            throw new BaseException(jsonObject.getStr("errmsg"));

        }

        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");

    }


    /**
     * 获取token
     *
     * @return
     */
    private String getToken() {
        //创建一个集合
        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);


        //发起请求
        String result = HttpUtil.get(TOKEN_URL, paramMap);

        //结果是一个Map
        JSONObject jsonObject = JSONUtil.parseObj(result);


        //判断接口响应是否出错
        if (ObjectUtils.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }

        String token = jsonObject.getStr("access_token");

        return token;

    }
}
